package com.yun.zf.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfCustomerMapper;
import com.yun.zf.admin.mapper.ZfCustomerRequirementMapper;
import com.yun.zf.admin.service.ZfCustomerAgentService;
import com.yun.zf.admin.service.ZfPactCertService;
import com.yun.zf.admin.service.ZfCustomerService;
import com.yun.zf.common.admin.dto.customer.CustomerBasicInfo;
import com.yun.zf.common.admin.dto.customer.CustomerListDto;
import com.yun.zf.common.admin.model.customer.CustomerBasicMo;
import com.yun.zf.common.admin.model.pact.PactPersonMo;
import com.yun.zf.common.app.domain.ZfCustomer;
import com.yun.zf.common.app.domain.ZfCustomerAgent;
import com.yun.zf.common.constant.Constants;
import com.yun.zf.common.esIndex.CustomerIndexTemplate;
import com.yun.zf.common.mapstruct.admin.CustomerMapper;
import com.yun.zf.system.security.utils.SecurityUtils;
import com.yun.zf.system.utils.EsRestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户资源表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-03-30
 */
@Service
public class ZfCustomerServiceImpl extends ServiceImpl<ZfCustomerMapper, ZfCustomer> implements ZfCustomerService {

    @Autowired
    private ZfCustomerAgentService customerAgentService;
    @Autowired
    private ZfPactCertService certService;
    @Autowired
    private EsRestUtil esRestUtil;
    @Autowired
    private ZfCustomerRequirementMapper requirementMapper;
    /**
     * 新增客户基本信息
     * @return
     */
    @Override
    @Transactional
    public int addCustomer(CustomerBasicMo basicMo, int agentNo ,Long proCode, Long cityCode) {
      ZfCustomer customer =  CustomerMapper.INSTANCE.basicMoToCustomer(basicMo);
      customer.setCreateTime(new Date());
      customer.setProvinceCode(proCode);
      customer.setCityCode(cityCode);
        // 首次新增默认为已带看
        customer.setStatus("looked");
        //1、 录入客户信息
      int res =  baseMapper.insert(customer);
      //2、录入客户业务员关系
      if ( res>0){
          ZfCustomerAgent customerAgent = new ZfCustomerAgent();
          customerAgent.setAgentNo(agentNo);
          customerAgent.setCustomerId(customer.getId());
          customerAgent.setProvinceCode(proCode);
          customerAgent.setCityCode(cityCode);
          customerAgent.setCreateTime(new Date());
          customerAgent.setReference(-1);
          customerAgent.setAgentName(SecurityUtils.getLoginUser().getAgentName());
          customerAgentService.addCustomerAgent(customerAgent);
      }
        CustomerIndexTemplate customerIndexTemplate = new CustomerIndexTemplate();
        customerIndexTemplate.setCustomerId(customer.getId().toString());
        customerIndexTemplate.setCityCode(customer.getCityCode());
        customerIndexTemplate.setProvinceCode(customer.getProvinceCode());
        customerIndexTemplate.setPhone(customer.getPhone());
        customerIndexTemplate.setStatus(customer.getStatus());
        customerIndexTemplate.setSpace("privately");
        customerIndexTemplate.setAgentNo(agentNo);
        customerIndexTemplate.setName(basicMo.getName());
         esRestUtil.addDataRest(customerIndexTemplate, Constants.ES_CUSTOMER_INDEX, customer.getId().toString());
        return res;
    }

    /**
     * 补充客户资料信息
     * @return
     */
    @Override
    @Transactional
    public int motifyCustomer(CustomerBasicMo basicMo, int  agentNo,Long proCode, Long cityCode ) {
        QueryWrapper<ZfCustomer> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ZfCustomer::getId,basicMo.getId())
                .eq(ZfCustomer::getCityCode,cityCode).eq(ZfCustomer::getProvinceCode,proCode);
        ZfCustomer customer =  baseMapper.selectOne(wrapper);
        BeanUtil.copyProperties(basicMo, customer, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
      int cn =   baseMapper.update(customer,wrapper);

        // 推送es
        CustomerIndexTemplate customerIndexTemplate = new CustomerIndexTemplate();
        customerIndexTemplate.setCustomerId(customer.getId().toString());
        customerIndexTemplate.setCityCode(customer.getCityCode());
        customerIndexTemplate.setProvinceCode(customer.getProvinceCode());
        customerIndexTemplate.setPhone(customer.getPhone());
        customerIndexTemplate.setStatus(customer.getStatus());
        customerIndexTemplate.setSpace("privately");
        esRestUtil.addDataRest(customerIndexTemplate, Constants.ES_CUSTOMER_INDEX, customer.getId().toString());

        return  cn;
    }

    public ZfCustomer getCustomerInfo(Long proCode, Long cityCode, Long customerId){
        QueryWrapper<ZfCustomer> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.select("id","province_code","city_code","phone","name","source_type").eq("city_code",cityCode).eq("province_code",proCode)
                .eq("id",customerId);
        return  baseMapper.selectOne(customerQueryWrapper);
    }

    @Override
    public CustomerBasicMo getBasicInfo(Long proCode, Long cityCode, Long customerId) {
      CustomerBasicMo mo =  baseMapper.getBasicInfo(proCode,cityCode,customerId);
      // 获取证件信息

        return mo;
    }

    /**
     * 获取客户手机号码
     * @param proCode
     * @param cityCode
     * @param customerId
     * @return
     */
    @Override
    public String getPhone(Long proCode, Long cityCode, Long customerId) {

        return baseMapper.getPhone(proCode,cityCode,customerId);
    }

    @Override
    public CustomerBasicInfo getPactCustomer(Long proCode, Long cityCode, Long customerId) {
        return baseMapper.getPactCustomer(proCode, cityCode, customerId);
    }

    @Override
    public List<CustomerListDto> getSearchList(Long proCode, Long cityCode, List<Long> ids) {
        List<CustomerListDto> list =  baseMapper.getName(proCode,cityCode,ids);
        for (CustomerListDto c: list
             ) {
            CustomerListDto r =     requirementMapper.getReqBasic(proCode,cityCode,Long.parseLong(c.getId()));
            if (r!=null){
                c.setArea(r.getArea());
                c.setBedrooms(r.getBedrooms());
                c.setBudget(r.getBudget());
                c.setDemandType(r.getDemandType());
                c.setRemark(r.getRemark());
                c.setUnit(r.getUnit());
                c.setModifiedTime(r.getModifiedTime());

            }
        }
        return  list;
    }
}
